package org.jeecg.modules.demo3.fly.entity.tcp;


import org.jeecg.modules.demo3.fly.utils.Base_Conversion;
import org.jeecg.modules.demo3.fly.utils.DateShift;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TCPServer {
    private ServerSocket serverSocket;
    /**
     * 创建线程池来管理客户端的连接线程
     * 避免系统资源过度浪费
     */
    private ExecutorService exec;

    //public static ConcurrentHashMap<String, Clents> concurrentHashMap = new ConcurrentHashMap();


    public static List<Clents> clentsList = new ArrayList<>();
    //  String Serial_number;
    private int serial_Number;
    DateShift dateShift = new DateShift(); //数据解析类

    public TCPServer() {

    }

    /**
     * 无人机端步骤，为对应的无人机发送识别信息至服务器端并实现无人机与服务器端的数据透传
     * 服务器端步骤，实现无人机端与地面站端之间的数据通信
     * 地面站端步骤，为对应的地面站发送识别信息至服务器端并实现地面站与服务器端的数据透传；
     */
    public void tcpConnect() {

        try {
            serverSocket = new ServerSocket(2222);//开启服务的端口，需和客户端一致
            exec = Executors.newCachedThreadPool();
            while (true) {
                byte[] bytes = new byte[1024];
                Socket socket = serverSocket.accept(); //对客户端的访问进行监听
                String hostAddress = socket.getInetAddress().getHostAddress();
                System.out.println("客户端的IP：=====" + socket.getInetAddress().getHostAddress());
                InputStream inputStream = socket.getInputStream();
                int readLenght = inputStream.read(bytes, 0, bytes.length);
                p(bytes, readLenght);
                Clents clents = new Clents();
                System.out.println("接收的序列号为：==" + Arrays.toString(bytes));
                clents.setNumber(serial_Number);
                clents.setSocket(socket);
                System.out.println("clents.toString()===============" + clents.toString());
                clentsList.add(clents);
                // concurrentHashMap.put(hostAddress, clents);
                //  exec.execute(new SocketClients(socket));
                exec.execute(new SocketClients(clents));
                System.out.println("连接的客户端数量：" + clentsList.size());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void p(byte[] bytes, int readLenght) {
        if (bytes[0] == (byte) 0x09 && bytes[1] == (byte) 0xD7) {//飞控连接
            //    if (bytes[0] == (byte) 0xAA && bytes[1] == (byte) 0x63) {//飞控连接
            DistributionDataBean distributionDataBean = dateShift.issue_Agreement(bytes, readLenght); // 数据解析
            if (distributionDataBean != null) {
                serial_Number = distributionDataBean.getSerial_number();
            }
        } else {//客户端连接
          //  byte[] serialNumberBytes = new byte[readLenght];

            String s = Base_Conversion.bytesToHex(bytes);
            System.out.println("s==============" + s);
            serial_Number = Base_Conversion.byte3intLittleEndian(bytes);
            // Serial_number = String.valueOf(Base_Conversion.byte2intLittleEndian(bytes));
            System.out.println("----------else--------------" + serial_Number);

        }
    }

}
